package sourceapi.client.mainpage;

import java.util.ArrayList;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.URL;
import com.google.gwt.json.client.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class SourceCode extends Base {
	
	private static  ArrayList<SourceCode> SavedData = new ArrayList<SourceCode>();
	
	String Name = "";
	String Source = "";

	public SourceCode( JSONValue Val )
	{
		
		JSONObject Values = Val.isObject();
		
		if( Values == null )
			return;
	
		this.Name = Values.get("name").isString().stringValue();
		this.Source = Values.get("content").isString().stringValue();
		
		SavedData.add( this );
		
	}
	
	public void DisplayPage(){
		
		RootPanel Screen = RootPanel.get("MainBody");
		
		Screen.clear();
		
		VerticalPanel FrameHolder = new VerticalPanel( );
		FrameHolder.getElement().getStyle().setMarginLeft(10.0, Unit.PX );
		
		FrameHolder.add( Base.GetHeader( this.Name ) );
		FrameHolder.add( new HTML( "<pre>" + this.Source + "</pre>" ) );
		
		Screen.add( FrameHolder );
		
	}
	
	public static void Display( final String Name ){
		
		//Do not let a request that is no longer needed keep going
		CancelRequest();
		
		if( DisplayByName( Name ) == true )
			return;
		
		//Page is not yet cached, we must go to the web and find it!
		String url = GWT.getHostPageBaseURL() + 
			"data/getsource.php?id=" + Name;
		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
		
		try {		
			builder.setCallback( new RequestCallback(){

				public void onError(Request request, Throwable exception) {
					Window.alert( exception.getMessage() );
				}

				public void onResponseReceived(Request request, Response response) {
					if (200 == response.getStatusCode()) {
						new SourceCode( JSONParser.parse( response.getText() ) );
						DisplayByName( Name );
					} else {
						//TODO: Show Error
					}
				}
				
			} );
			PageRequest = builder.send();
		} catch (RequestException e) {
		  // Couldn't connect to server        
		}	
		
		
		
	}
	
	public static boolean DisplayByName( String Name ){
		
		Iterator<SourceCode> ListIter = SavedData.iterator();
		
		//Find if the page already is cached
		while( ListIter.hasNext() ){
			
			SourceCode item = ListIter.next();
			
			if( Name.equalsIgnoreCase( item.Name ) ){
				item.DisplayPage();
				return true;
			}
			
		}
		
		return false;
		
	}
	
}
